package com.xuyuan.io;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RandomAccessTest {
    private static final String FILE_PATH = "C:/Software/IDEA2022/workspace/reflect/classes" + File.separator + "json.txt";
    private static final String FILE_PATH1 = "C:/Software/IDEA2022/workspace/reflect/classes" + File.separator + "test5.txt";
    private static final String LINE_SEPARATOR = System.lineSeparator();

    @Test
    public void test() throws IOException {
        RandomAccessFile raf = new RandomAccessFile(FILE_PATH, "rw");
        String line;
        while ((line = raf.readLine()) != null) {
            System.out.println(line);
        }
    }

    @Test
    public void test1() throws IOException {
        RandomAccessFile raf = new RandomAccessFile(FILE_PATH1, "rw");
        // 写入一些文本数据
        raf.writeBytes("Hello, World!" + LINE_SEPARATOR);
        raf.writeBytes("This is a test." + LINE_SEPARATOR);
        raf.writeBytes("RandomAccessFile example." + LINE_SEPARATOR);

        // 写入一个整数
        // int number = 12345;
        // raf.writeInt(number);

        // 写入一个浮点数
        // double value = 3.14159;
        // raf.writeDouble(value);
    }

    @Test
    public void test2() throws IOException {
        RandomAccessFile raf = new RandomAccessFile(FILE_PATH1, "r");
        // 读取文本数据
        String line;
        while ((line = raf.readLine()) != null) {
            System.out.println(line);
        }

        // 读取一个整数
        // int number = raf.readInt();
        // System.out.println("Read integer: " + number);

        // 读取一个浮点数
        // double value = raf.readDouble();
        // System.out.println("Read double: " + value);
    }

    // @Test
    // public void test3() throws IOException {
    //     System.out.println(File.separator);
    //     System.out.println(File.pathSeparator);
    //     String s = AccessController.doPrivileged(
    //             new GetPropertyAction("path.separator"));
    //     System.out.println(s);
    // }
}
